package com.yanqu.road.server.cmd.activity.commonteam;


import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityRewardResult;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.pb.activity.CommonTeamProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@CmdApi(reqMsg = CommonTeamProto.CommonTeamGetRewardReqMsg.class, respMsg = CommonTeamProto.CommonTeamGetRewardRespMsg.class)
@Cmd(code = Protocol.S_COMMON_TEAM_RECEIVE_TEAM_REWARD, desc = "通用组队-领取队伍榜单奖励")
public class CommonTeamReceiveRankRewardCmd extends AbstractCommand {


    private int activityId;
    private int conditionId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CommonTeamProto.CommonTeamGetRewardReqMsg rewardReqMsg = CommonTeamProto.CommonTeamGetRewardReqMsg.parseFrom(packet.getBytes());
        activityId = rewardReqMsg.getActivityId();
        conditionId = rewardReqMsg.getConditionId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        CommonTeamProto.CommonTeamGetRewardRespMsg.Builder respMsg = CommonTeamProto.CommonTeamGetRewardRespMsg.newBuilder();
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if (activityInfo == null) {
            respMsg.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            player.sendPacket(Protocol.U_COMMON_TEAM_RECEIVE_TEAM_REWARD, respMsg);
            return;
        }
        if (!ActivityMgr.activityInRewardPeriod(activityInfo)) {
            respMsg.setRet(GameErrorCode.E_ACTIVITY_NO_IN_GET_REWARD_TIME);
            player.sendPacket(Protocol.U_COMMON_TEAM_RECEIVE_TEAM_REWARD, respMsg);
            return;
        }
        //发送到跨服
        ActivityRewardResult rewardResult = player.getModule(ActivityModule.class).geCommonTeamRankActivityReward(activityId, conditionId, false, 0);
        if (rewardResult.getRet() != 0) {
            respMsg.setRet(rewardResult.getRet());
            player.sendPacket(Protocol.U_COMMON_TEAM_RECEIVE_TEAM_REWARD, respMsg);
        }
    }

    @Override
    public void sendData(GamePlayer player) {}
}
